﻿using System.Text.Json;
using System.Text.RegularExpressions;

namespace zijian666.WebApi.JsonOption
{
    /// <summary>
    /// 自定义JSON名称转换器
    /// </summary>
    class JsonNameLowercasePolicy : JsonNamingPolicy
    {
        public static JsonNameLowercasePolicy LowerCase { get; } = new();

        private static readonly Regex _regex = new("(?<!(^|[A-Z]))(?=[A-Z])", RegexOptions.Compiled);

        public override string ConvertName(string name) => _regex.Replace(name, "_").ToLowerInvariant();
    }
}
